package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.DamageListDao;
import com.atguigu.jxc.dao.DamageListGoodsDao;
import com.atguigu.jxc.entity.DamageList;
import com.atguigu.jxc.entity.DamageListGoods;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.DamageListGoodsService;
import com.atguigu.jxc.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings("all")
public class DamageListGoodsServiceImpl implements DamageListGoodsService {
    @Autowired
    private DamageListGoodsDao damageListGoodsDao;

    @Autowired
    private DamageListDao damageListDao;


    @Override
    @Transactional
    public void save(DamageList damageList, List<DamageListGoods> damageListGoods) {
        //获取主键
        damageListDao.save(damageList);
        //
        damageListGoods.forEach(a -> {
            a.setDamageListId(damageList.getDamageListId());
        });
        damageListGoods.forEach(a -> {
            damageListGoodsDao.save(a);
        });

    }

    @Override
    public Map<String, Object> list(String sTime, String eTime) {
        Map<String, Object> result = new HashMap<>();
        List<DamageList> list = damageListDao.list();
         List<DamageList> collect = list.stream().filter(a -> {
            try {
                return DateUtil.getTimeSlotByDay(sTime, eTime).contains(a.getDamageDate());
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }).collect(Collectors.toList());
        result.put("rows", collect);
        return result;
    }

    @Override
    public Map<String, Object> goodsList(Integer damageListId) {
        Map<String, Object> result = new HashMap<>();
        List<DamageListGoods> list = damageListGoodsDao.listAlarm(damageListId);
        result.put("rows", list);
        // result.put("total", list.size());
        return result;
    }
}
